<?php

class GCD {

    var $__sql;
    var $__resultSet;
    var $__columnInfo;
    var $__returnRows;
    var $__lastquery;
    var $__tblLog = "audit_trail";
    var $__fldTblLog = array("log_time", "log_aktor", "log_event");

    function insertLog($arrVals) {
        $tablename = $this->__tblLog;
        $arrFields = $this->__fldTblLog;
        $this->insertData($tablename, $arrFields, $arrVals);
    }

    function set($sql) {
        $this->__sql = $sql;
        $this->__resultSet = array();
        $this->__columnInfo = array();
        $this->__returnRows = 0;
    }
    
    function getAndSet($sql) {
        $this->__sql = $sql;
        $this->__resultSet = array();
        $this->__columnInfo = array();
        $this->__returnRows = 0;
    }

    function __construct($sql = "") {
        $this->set($sql);
        $this->__resultSet = array();
        $this->__columnInfo = array();
        $this->__returnRows = 0;
    }

    function export($file) {
        global $dbi, $dbi_slave;
        $sql = "COPY (" . $this->__sql . ") TO '" . $file . "' WITH CSV HEADER ";

        $dbi->query_with_no_result($sql);
    }

    function execute($sql) {
        global $dbi, $dbi_slave;
        $dbi->query_with_no_result($sql);
    }

    function getResultSet($whr = "", $sortorder = "", $limit = "10", $offset = "0") {
        global $dbi, $dbi_slave;
        if ($sortorder == "")
            $sortorder = "";
        $sql = $this->__sql . ($whr == "" ? "" : " where " . $whr) .
                ($sortorder == "" ? "" : " order by " . $sortorder);
        //($limit < 1?"":" limit ". $limit);
        $this->__lastquery = $sql;
        //echo $sql;
        $row = $dbi_slave->query($sql, $limit, $offset);
        $this->__returnRows = $dbi_slave->lastaffectedrows;
        $this->__resultSet = $row;
    }

    function getColumnInfo() {
        global $dbi, $dbi_slave;
        $row = $dbi->get_column_info($this->__sql);
        $this->__lastquery = $this->__sql;
        $this->__columnInfo = $row;
    }

    function listData() {
        return $this->__resultSet;
    }

    function listColumnName() {
        return $this->__columnInfo["column_name"];
    }

    function listColumnType() {
        return $this->__columnInfo["column_type"];
    }

    function countListData() {
        return $this->__returnRows;
    }

    function checkDuplicateKeyByConds($conds) {
        global $dbi, $dbi_slave;
        $sql = $this->__sql . " where " . $conds;
        $this->__lastquery = $sql;
        $row = $dbi->query($sql);
        if (count($row) < 1) {
            $out = false;
        } else {
            $out = true;
        }

        return $out;
    }

    function checkDuplicateKey($keyField, $valueField) {
        global $dbi;
        $sql = $this->__sql . " where " . $keyField . " = '" . $valueField . "'"; 
        $this->__lastquery = $sql;
        $row = $dbi->query($sql);
        if (count($row) < 1) {
            $out = false;
        } else {
            $out = true;
        }
        return $out;
    }

    function getSingleData($where) {
        global $dbi;

        $sql = $this->__sql . " where " . $where;
        $this->__lastquery = $sql;
        $row = $dbi->get_row($sql);

        return $row;
    }

    function insertData($tablename, $arrFields, $arrVals, $newkode = false, $fldseq = "") {
        global $dbi, $log, $auth;
        $contentnewcode = "";
        $dbi->insert($tablename, $arrFields, $arrVals);
        $this->__lastquery = $dbi->lastquery;
        if ($newkode) {
            $contentnewcode = $dbi->last_index($tablename . "_" . $fldseq . "_seq");
        }
        return $contentnewcode;
    }

    function updateData($tablename, $arrFields, $arrVals, $conds) {
        global $dbi, $log, $auth;
        $dbi->update($tablename, $arrFields, $arrVals, $conds);
        $this->__lastquery = $dbi->lastquery;
    }

    function deleteAllData($tablename, $conds) {
        global $dbi, $log, $auth;

        $dbi->delete($tablename, $conds);
        $this->__lastquery = $dbi->lastquery;
    }

    function deleteData($tablename, $fields, $nilai) {
        global $dbi, $log, $auth;
        $kondisi = " " . $fields . "='" . $nilai . "' ";
        $dbi->delete($tablename, $kondisi);
        $this->__lastquery = $dbi->lastquery;
    }
    
    public static function getRecordCount($tableName, $doAnalyze = false){
        global $dbi;
        if ($doAnalyze)
            $dbi->query_with_no_result("analyze ".$tableName);
        $fieldselected = "jumlah";
        return $dbi->get_row("select reltuples::integer jumlah from pg_class where relname='".$tableName."'")->$fieldselected;
    }
    
    public static function getGroupAll($fieldName){
        global $idGroupAll;
        return $fieldName." in (".$idGroupAll.") ";
    }
    
    public static function getGroupLess($fieldName){
        global $idGroupLess;
        return $fieldName." id_group in (".$idGroupLess.") ";
    }

}

?>